package com.example.wx.dailyreport.controller;


import com.example.wx.dailyreport.pojo.ReportRoles;
import com.example.wx.dailyreport.pojo.User;
import com.example.wx.dailyreport.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hulinbo
 * @since 2020-07-28
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/add")
    public Map<String, Object> addUser(User user) {
        user.setSurvival(1);
        Map<String, Object> map = new LinkedHashMap<>();
        if (userService.save(user))
            map.put("status", 200);
        else
            map.put("status", 400);
        return map;
    }

    @GetMapping("/checkusername")
    public Map<String, Object> checkUsername(String username) {
        Map<String, Object> map = new LinkedHashMap<>();
        User admin = userService.findByUsername(username);
        if (admin == null)
            //用户名可用
            map.put("status", 200);
        else
            //用户名不可用
            map.put("status", 400);
        return map;
    }

    @GetMapping("/getuserinfo")
    public Map<String, Object> getUserInfo() {
        Map<String, Object> map = new LinkedHashMap<>();
        User user = userService.findByUsername((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        LinkedList<GrantedAuthority> authorities = new LinkedList<>(SecurityContextHolder.getContext().getAuthentication().getAuthorities());
        String role = authorities.getFirst().getAuthority();
        map.put("status", 200);
        map.put("name", user.getName());
        map.put("role", role);
        return map;
    }

    @GetMapping("/getuserclassinfo")
    public Map<String, Object> getUserClassInfo() {
        Map<String, Object> map = new LinkedHashMap<>();
        ReportRoles reportRoles = userService.getUserClassInfo((String) SecurityContextHolder.getContext().getAuthentication().getPrincipal());
        map.put("status", 200);
        map.put("data", reportRoles);
        return map;
    }


    @PostMapping("/updatepassword")
    public Map<String, Object> updatePassword(@RequestParam String password) {
        Map<String, Object> map = new LinkedHashMap<>();
        String username = null;
        username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (StringUtils.isNotEmpty(username) && password != null && password.length() > 4) {
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            if (userService.updatePassword(user)) {
                //修改成功
                map.put("status", 200);
            }
        } else {
            map.put("status", 400);
        }
        return map;
    }

    @PostMapping("/updatename")
    public Map<String, Object> updateName(@RequestParam String name) {
        Map<String, Object> map = new LinkedHashMap<>();
        String username = null;
        username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (StringUtils.isNotEmpty(username) && name != null && name.length() > 0) {
            User user = new User();
            user.setUsername(username);
            user.setName(name);
            if (userService.updateName(user)) {
                //修改成功
                map.put("status", 200);
            }
        } else {
            map.put("status", 400);
        }
        return map;
    }

}

